#import "@{nameOfClass}.h"
@{importChildHs}
@implementation @{nameOfClass}
@synthesize @{id} = _@{id};
@synthesize parentController = _parentController;
@synthesize currentContainer=_currentContainer;
@synthesize headerView = _headerView;
@synthesize contextDictionary = _contextDictionary;
@synthesize ctx = _ctx;
@synthesize ctx1 = _ctx1;
@synthesize ctx2 = _ctx2;
@{umcontextSynthesize}
@{addChildSynthesizes}
@{controllerC}
@{namespaceC}
@{context}

- (id)initWithParentCtl:(UIViewController*) parentCtl
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        if(parentCtl){
            self.parentController = parentCtl;
        }else{
            self.parentController = self;
        }
        _@{id} = _viewObject;
        @{selfView}
    }
	return self;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}
- (void)viewDidLoad
{
	[super viewDidLoad];
	AppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
	_currentContainer = [appDelegate currentContainer];
	[_currentContainer initController:ModelName];
    @{umcontextInit}
    _ctx = [[UMEntityContext alloc]init];
}
- (void)initHeaderView:(NSInteger)section{
	@{initChilds}
    @{dataBinds}
	@{addChilds}
	@{initHeaderViews}

}
- (void)createHeader{
    @{initChilds}
    @{initHeaderViews}
    @{setviewDidLoads}
}
- (void)createContent{
    @{initChilds}
    @{initContentViews}
    @{setviewDidLoads}
}
- (void)initContentView:(NSIndexPath*)indexPath{
	@{initChilds}
    @{dataBinds}
    [self bindAll];
	@{addChilds}
	@{initContentViews}
	@{setviewDidLoads}
}

-(void)initHeightForRow:(NSIndexPath *)indexPath
{
    @{initHeightForRows}
}

@{header_flag}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    _ctx = [_context getAtIndex:section];
	[self initHeaderView:section];
    _headerView.tag = section+8000;
	return _headerView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return [_context getCount];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString* str = [[_context getAtIndex:section] getValue:@"@{headerField}"];
    UMContextCollection* ccc = [[UMContextCollection alloc]initWithJSON:str];
    return [ccc getCount];
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    [self.contentViews removeAllObjects];
    NSString* str = [[_context getAtIndex:indexPath.section] getValue:@"@{headerField}"];
    //UMContextCollection* ccc = [[UMContextCollection alloc]initWithJSON:str];

    _ctx = [[[_context getAtIndex:indexPath.section] getObject:@"@{headerField}"] getAtIndex:indexPath.row];
    //_ctx = [ccc getAtIndex:indexPath.row];
    [self initContentView:indexPath];
    if ([_ctx getValue:@"ListViewSelector"].length == 0) {
        int index = 1;
        if(index < [self.contentViews count])
            self.contentView = [self.contentViews objectAtIndex:index];
    }else{
        NSString* templateIndex = [_ctx getValue:@"ListViewSelector"];
        int index = [templateIndex intValue];
        if (index < 0||index >= [self.contentViews count]) {
            index = 1;
        }
        if(index < [self.contentViews count])
            self.contentView = [self.contentViews objectAtIndex:index];
    }
    return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    [self createHeader];
    return [super tableView:tableView heightForHeaderInSection:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.contentViews removeAllObjects];
    NSString* str = [[_context getAtIndex:indexPath.section] getValue:@"@{headerField}"];
    //UMContextCollection* ccc = [[UMContextCollection alloc]initWithJSON:str];
    _ctx = [[[_context getAtIndex:indexPath.section] getObject:@"@{headerField}"] getAtIndex:indexPath.row];
    //_ctx = [ccc getAtIndex:indexPath.row];
    [self createContent];
    if ([_ctx getValue:@"ListViewSelector"].length == 0) {
        int index = 1;
        if(index < [self.contentViews count])
            self.contentView = [self.contentViews objectAtIndex:index];
    }else{
        NSString* templateIndex = [_ctx getValue:@"ListViewSelector"];
        int index = [templateIndex intValue];
        if (index < 0||index >= [self.contentViews count]) {
            index = 1;
        }
        if(index < [self.contentViews count])
            self.contentView = [self.contentViews objectAtIndex:index];
    }
    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
#else
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    self.dataCount = [_context getCount];
    return [_context getCount];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)_editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UMPListViewEditingStyleDeleteSingle) {
        //�����ゆ�版��婧���版��

         [super tableView:tableView commitEditingStyle:_editingStyle forRowAtIndexPath:indexPath];

        //[_context removeCtxAtIndex:indexPath.row];
        // [tableView beginUpdates];
        //[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        //[tableView endUpdates];
        // [tableView reloadData];

    }
    
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    [self.contentViews removeAllObjects];
    _ctx = [_context getAtIndex:indexPath.row];
    [self initContentView:indexPath];
     if ([_ctx getValue:@"ListViewSelector"].length == 0) {
        int index = 0;
        if(index < [self.contentViews count])
            self.contentView = [self.contentViews objectAtIndex:index];
    }else{
        NSString* templateIndex = [_ctx getValue:@"ListViewSelector"];
        int index = [templateIndex intValue];
        if (index < 0||index >= [self.contentViews count]) {
            index = 0;
        }
        if(index < [self.contentViews count])
            self.contentView = [self.contentViews objectAtIndex:index];
    }

    return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.contentViews removeAllObjects];
    _ctx = [_context getAtIndex:indexPath.row];
    //[self initContentView:indexPath];
    [self initHeightForRow:indexPath];
     if ([_ctx getValue:@"ListViewSelector"].length == 0) {
        int index = 0;
        if(index < [self.contentViews count])
            self.contentView = [self.contentViews objectAtIndex:index];
    }else{
        NSString* templateIndex = [_ctx getValue:@"ListViewSelector"];
        int index = [templateIndex intValue];
        if (index < 0||index >= [self.contentViews count]) {
            index = 0;
        }
        if(index < [self.contentViews count])
            self.contentView = [self.contentViews objectAtIndex:index];
    }

    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
#endif
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidUnload
{
	[super viewDidUnload];
}
-(void)onInit{
    [_currentContainer onInit:controllerC];
}
-(void)onLoad{
    [_currentContainer onLoad:controllerC];
}
-(void)onDataBinding{
    //here do databinding
    NSString *context = [_currentContainer getModel:ModelName];
    _contextDictionary = [context objectFromJSONString];
    if(_contextDictionary != nil)
        [self dataBind:[_contextDictionary JSONString]];
}
-(void)onComplete{
    [_currentContainer onComplete:controllerC];
}

- (void)dataBind:(NSString*)data{
    if(data !=nil){
        @{umcontextCreate}
        [self reloadAllData];
    }
}
- (void)dataBind2:(UMContextCollection*)thedata cursor:(NSString *)thecursor{
    if(thedata !=nil){
        _context = thedata;//_context = [[UMContextCollection alloc]initWithJSON:thedata];
        UMEntityContext *parentctx = [self.parentController valueForKey:@"context"];
        ICursor *ic = [parentctx getCursor:thecursor];
        if(self.dataCount>0&&self.indexClick&&self.dataCount==[thedata getCount])
        {
            //��锋�颁��琛�
            [(UITableView *)self.viewObject.content reloadRowsAtIndexPaths:[NSArray arrayWithObjects:self.indexClick, nil] withRowAnimation:UITableViewRowAnimationFade];
            
        }
//        else if (ic && ic.CurrentIndex != -1 && ic.CurrentGroup != -1) {
//            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:ic.CurrentIndex inSection:ic.CurrentGroup];
//            //刷新一行
//            [(UITableView *)self.viewObject.content reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
//        }
        else
        {
            [self reloadAllData];
        }
    }
}
-(id)valueByBindfield:(NSString*)bindingfield
{
    return [_contextDictionary objectForKey:bindingfield];
}
- (void)collectionData:(UMEntityContext*) ttt{
    NSString* key =  [[self.viewObject.bindInfo.controlFieldPair allKeys] objectAtIndex:0];
    NSArray* values = [self.viewObject.bindInfo.controlFieldPair valueForKey:key];
    NSString* value = [values objectAtIndex:0];
    [ttt setValue:value source:_context.currentJson];
}
@end